package com.cn.zpark.dbcp;

import com.cn.zpark.utils.DBCPUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * @PackageName: com.cn.zpark.dbcp
 * @ClassName: DBCPSelect
 * @Description:
 * @author: RZS
 * @date: 2022/4/1  8:04
 */
public class DBCPSelect {
    public static void main(String[] args) {
        // 准备连接信息
        Connection conn = null;
        PreparedStatement stat = null;
        ResultSet res = null;

        try {
            // 获取数据库连接对象
            conn = DBCPUtils.getConn();
            // 创建sql语句
            String sql = "select id, uname, money from tab2";
            // 创建预编译sql对象
            stat = conn.prepareStatement(sql);
            // 执行sql
            res = stat.executeQuery();
            // 遍历结果集
            while (res.next()) {
                // 取出每一列的值
                System.out.println(res.getInt("id"));
                System.out.println(res.getString("uname"));
                System.out.println(res.getDouble("money"));
                System.out.println("---------------------------------");
            }
        } catch(Exception e) {
            e.printStackTrace();
        }finally {
            // 关闭资源
            DBCPUtils.close(res, stat, conn);
        }

    }
}
